# -*- coding: utf-8 -*-
from imutils.video import VideoStream
from imutils.video import FPS
import numpy as np
import time
import cv2
from kafka import KafkaConsumer,KafkaProducer
import sys
import pickle

from detection.det_model import det_model

def showCam(server, topic, ptopic):

    consumer = KafkaConsumer(
        topic,
        bootstrap_servers=[server])

    producer = KafkaProducer(bootstrap_servers=server)
    time.sleep(2.0)


    for msg in consumer:
        decoded = np.frombuffer(msg.value, np.uint8)
        print(decoded.size)
#         img = decoded.reshape((int)(decoded.size / (700 * 3)), 700, 3)
        img = decoded.reshape((int)(decoded.size / (400 * 3)), 400, 3)
        dets = det_model.pred_img(img)
        producer.send(ptopic, key= msg.key ,value=pickle.dumps(dets)  )




if __name__ == '__main__':
    server = 'kafka:9092'
    topic = 'rtsp'
    ptopic = 'pickle'
    showCam(server, topic,ptopic)

